import request from '@api/request';
import { HttpUrl } from '@api/httpUrl';
import { useUserStore } from '@/store/modules/user';

// 获取全部产品列表
export function getAllProductList() {
    return request.get<{ customerId: string; products: AjaxResponse['productList'] }[]>(HttpUrl.getAllProductList);
}

// 获取产品列表
export function getProductList(customerId: string) {
    return request.get<AjaxResponse['productList']>(HttpUrl.getProductList, { params: { customerId } });
}

// 获取产品信息
export function getProductInfo(customerId: string, productId: string) {
    return request.get<AjaxResponse['productList'][0]>(HttpUrl.getProductInfo, { params: { customerId, productId } });
}

// 查询产品的库存
export function getProductStock(customerId: string, productId: string) {
    return request.get<number>(HttpUrl.getProductStock, { params: { customerId, productId } });
}

// 查找产品列表
export function searchProductList(searchValue: string) {
    return request.get<{ products: AjaxResponse['productList'] }[]>(HttpUrl.searchProductList, { params: { searchValue } });
}

// 排序产品顺序
export function reorderProductPriority(customerId: string) {
    return request.get(HttpUrl.reorderProductPriority, { params: { customerId } });
}

// 产品名称检测
export function checkProductName(customerId: string, productName: string) {
    return request.get(HttpUrl.checkProductName, { params: { customerId, productName } });
}

// 创建产品
export function createProduct(productForm: AjaxRequest['productForm']) {
    const useUser = useUserStore();
    return request.post(HttpUrl.createProduct, { ...productForm, creator: useUser.username, creatorId: useUser.id });
}

// 删除产品图稿
export function deleteProductDesign(
    type: 'all' | 'designFile' | 'thumbnail',
    customerId: string,
    customerName: string,
    productId: string,
    productDesignIndex: number,
    productName: string
) {
    const useUser = useUserStore();
    return request.del(HttpUrl.deleteProductDesign, {
        params: { type, customerId, customerName, productId, productDesignIndex, productName, creator: useUser.username, creatorId: useUser.id },
    });
}

// 更新产品
export function updateProduct(customerId: string, customerName: string, productData: AjaxRequest['updateProduct']) {
    const useUser = useUserStore();
    return request.put(HttpUrl.updateProduct, { customerId, customerName, productData, creator: useUser.username, creatorId: useUser.id });
}

// 录入产品
export function importProduct(customerId: string, productList: AjaxRequest['productForm']['product'][]) {
    return request.post(HttpUrl.importProduct, { customerId, productList });
}

// 恢复产品
export function recoverProduct(customerId: string, selected: string[]) {
    const useUser = useUserStore();
    return request.put(HttpUrl.recoverProduct, { customerId, selected, creator: useUser.username, creatorId: useUser.id });
}

// 删除产品
export function deleteProduct(customerId: string, customerName: string, selected: string[]) {
    const useUser = useUserStore();
    return request.put(HttpUrl.deleteProduct, { customerId, customerName, selected, creator: useUser.username, creatorId: useUser.id });
}

// 彻底删除产品
export function deleteProductCompletely(customerId: string, customerName: string, selected: string[]) {
    const useUser = useUserStore();
    return request.put(HttpUrl.deleteProductCompletely, { customerId, customerName, selected, creator: useUser.username, creatorId: useUser.id });
}
